package com.ruoyi.system.service.impl;

import com.ruoyi.system.entity.CatalogueTree;
import com.ruoyi.system.mapper.CatalogueTreeMapper;
import com.ruoyi.system.service.CatalogueTreeService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文件管理目录树(CatalogueTree)表服务实现类
 *
 * @author makejava
 * @since 2023-03-20 15:22:52
 */
@Service("catalogueTreeService")
public class CatalogueTreeServiceImpl implements CatalogueTreeService {
    @Resource
    private CatalogueTreeMapper catalogueTreeDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CatalogueTree queryById(Integer id) {
        return this.catalogueTreeDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param catalogueTree 筛选条件
     * @param pageRequest   分页对象
     * @return 查询结果
     */
    @Override
    public Page<CatalogueTree> queryByPage(CatalogueTree catalogueTree, PageRequest pageRequest) {
        long total = this.catalogueTreeDao.count(catalogueTree);
        return new PageImpl<>(this.catalogueTreeDao.queryAllByLimit(catalogueTree, pageRequest), pageRequest, total);
    }

    @Override
    public List<CatalogueTree> queryAll(CatalogueTree catalogueTree) {
        return catalogueTreeDao.queryAll(catalogueTree);
    }

    @Override
    public List<CatalogueTree> tree() {
        CatalogueTree catalogueTree = new CatalogueTree();
        catalogueTree.setPid(0);
        List<CatalogueTree> catalogueTrees = catalogueTreeDao.queryAll(catalogueTree);
        if (!CollectionUtils.isEmpty(catalogueTrees)) {
            catalogueTrees.forEach(value -> {
                CatalogueTree catalogueTreeCase = new CatalogueTree();
                catalogueTreeCase.setPid(value.getId());
                List<CatalogueTree> catalogueTreeList = catalogueTreeDao.queryAll(catalogueTreeCase);
                value.setTree(catalogueTreeList);
            });
        }
        return catalogueTrees;
    }

    /**
     * 新增数据
     *
     * @param catalogueTree 实例对象
     * @return 实例对象
     */
    @Override
    public CatalogueTree insert(CatalogueTree catalogueTree) {
        this.catalogueTreeDao.insert(catalogueTree);
        return catalogueTree;
    }

    /**
     * 修改数据
     *
     * @param catalogueTree 实例对象
     * @return 实例对象
     */
    @Override
    public CatalogueTree update(CatalogueTree catalogueTree) {
        this.catalogueTreeDao.update(catalogueTree);
        return this.queryById(catalogueTree.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.catalogueTreeDao.deleteById(id) > 0;
    }
}
